package DianShang_2024.ds_07.extract

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.lit

import java.text.SimpleDateFormat
import java.util.{Calendar, Properties}

object extract03 {
  def main(args: Array[String]): Unit = {
        val spark=SparkSession.builder()
          .master("local[*]")
          .appName("数据抽取第三题")
          .config("hive.exec.dynamic.partition.mode","nonstrict")
//          .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
//          .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
          .enableHiveSupport()
          .getOrCreate()

    val connect=new Properties()
    connect.setProperty("user","root")
    connect.setProperty("password","123456")
    connect.setProperty("driver","com.mysql.jdbc.Driver")

    val day:Calendar=Calendar.getInstance()
    val current_time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(day.getTime)
    day.add(Calendar.DATE,-1)
    val yesterday=new SimpleDateFormat("yyyyMMdd")

    spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_store?useSSL=false","base_province",connect)
      .createOrReplaceTempView("mysql_data")

    spark.table("ods07.base_province").createOrReplaceTempView("ods")

    spark.sql(
      """
        |select
        |m.*
        |from mysql_data as m
        |join ods as o
        |on o.id=m.i
        |where m.id not in(select id from ods)
        |""".stripMargin).withColumn("create_time",lit(current_time).cast("timestamp"))
      .createOrReplaceTempView("result")

    spark.sql(
      s"""
        |insert into table ods07.base_province
        |partition(etl_date="${yesterday}")
        |select * from result
        |""".stripMargin)








    spark.close()
  }

}
